package com.lin.missyou.dto.validators;

import com.lin.missyou.dto.PersonDTO;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 自定义注解
 * @author gcq
 * @Create 2021-10-06
 */
public class PasswordValidator implements ConstraintValidator<PasswordEqual, PersonDTO> {
    // ConstraintValidator
    // 参数一是对应注解
    // 参数二 自定义注解修饰的目标类型 修饰的是类 则传递类 修饰属性则传递属性
    private int min;
    private int max;

    /**
     * 注解参数验证
     * @param constraintAnnotation
     */
    @Override
    public void initialize(PasswordEqual constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    /**
     * 验证注解是否通过
     * @param personDTO
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(PersonDTO personDTO, ConstraintValidatorContext constraintValidatorContext) {
        String password1 = personDTO.getPassword1();
        String password2 = personDTO.getPassword2();
        // 针对参数再次进行校验
        return password1.equals(password2);
    }
}